﻿Public Class CINNERDELIVERYState : Inherits CStateBase

    ''' <summary>
    ''' 実行可能かを判断する
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function IsEnter(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERDELIVERYState.IsEnter")
        If CCommon.isInStr(task.ActionCd, "LABEL-ID") Then
            Return True
        Else
            Return False
        End If
    End Function

    ''' <summary>
    ''' 実行メソッド
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExecute(ByVal task As CTaskBase) As CStateBase
        Dim nextState As CStateBase
        Dim pdaTask As CPDATask = DirectCast(task, CPDATask)
        Debug.WriteLine("CINNERDELIVERYState.DoExecute")
        Dim readText As String = task.ProcessText


        pdaTask.WindowManager.SetMessage(CCommon.CNS_MSG_005, CCommon.COLOR_BLACK, CCommon.COLOR_GREEN)

        If pdaTask.checkParentLabel(CCommon.LABEL_DNC) Then ''ダミーでDNCラベルを入れる

            If pdaTask.chkOutQty() Then
                ''入り数チェック
                ''正常
                If pdaTask.commitLabel() Then
                    ''正常
                Else
                    ''error(通常ありえない）
                    'pdaTask.addWindowLabelBox(CCommon.LABEL_DNC, 2)
                    pdaTask.rollbackLabel()
                    'エラー
                    pdaTask.Hardware.warning()
                    ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                    SyncLock pdaTask.m_SendLockObject
                        Try
                            pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, CCommon.LABEL_DNC, "", "", "", "", 0))
                            pdaTask.C_FileCommon.SendTrxOrder()

                        Catch ex As Exception

                        End Try
                    End SyncLock

                    Dim result As String = ""
                    Dim prevWindowMode As EnumWindowMode
                    Do While result <> "[OK]"
                        If result = "" Then
                            prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                            pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_016, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                            pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                        End If
                        result = task.ReadTextWait(-1)
                        'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                    Loop
                    pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                    task.ErrorCd = "ER"
                    task.ProcessText = ""
                    nextState = GetNextState(task, GetType(CErrorState))
                    Return nextState

                End If
            Else
                ''入り数チェック
                ''エラー
                ''error
                'pdaTask.addWindowLabelBox(CCommon.LABEL_DNC, 2)
                pdaTask.rollbackLabel()
                'エラー
                pdaTask.Hardware.warning()
                ''pdaTask.delLastUniqueCd() ''登録したユニークコードを削除

                SyncLock pdaTask.m_SendLockObject
                    Try
                        pdaTask.C_FileCommon.AddTrxOrder(pdaTask.getSendErrorData(readText, CCommon.LABEL_DNC, "", "", "", "", 0))
                        pdaTask.C_FileCommon.SendTrxOrder()

                    Catch ex As Exception

                    End Try
                End SyncLock


                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(CCommon.CNS_MSG_025, CCommon.COLOR_WHITE, CCommon.COLOR_RED)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                    End If
                    result = task.ReadTextWait(-1)
                    'Debug.WriteLine("RESULT:{0},{1}", result, task.ProcessText)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)



                task.ErrorCd = "ER"
                task.ProcessText = ""
                nextState = GetNextState(task, GetType(CErrorState))
                Return nextState


            End If
        End If


        Dim idList() As CPDATask.strctInnerDelivery = Nothing
        Dim Cweb As CWebManager = CWebManager.GetInstance
        Dim altMsg As String = ""
        Dim resMsg As String = Cweb.GetInnerDeliveryList(CCommon.TrimCode(readText, CCommon.EnumGetString.LBL_ID_REPORT_CD), idList)
        Select Case resMsg
            Case "404"
                ''正しい出荷指示書ではありません
                altMsg = CCommon.CNS_MSG_041
            Case "410"
                ''正しい出荷指示書ではありません
                altMsg = CCommon.CNS_MSG_041
            Case "412"
                ''出荷指示書が古い
                altMsg = CCommon.CNS_MSG_042
            Case "500"
                ''システムエラー
                altMsg = CCommon.CNS_MSG_043
            Case "999"
                ''タイムアウト
                altMsg = CCommon.CNS_MSG_044
            Case Else
                ''不明
                altMsg = CCommon.CNS_MSG_043
        End Select
        If resMsg = "200" Then
            Dim i As Integer
            Dim labelState() As CPDATask.strctNowLabelState
            ReDim labelState(idList.Length - 1)
            Dim readDateTime As Date = Now()
            For i = 0 To idList.Length - 1
                labelState(i).LabelType = CCommon.LABEL_DNC ''idList(i).LabelType
                labelState(i).IsInMazda = False ''idList(i).IsInMazda ''DNCラベル読込時、Mの場合はTrue
                labelState(i).IsInToyo = False ''idList(i).IsInInoac
                labelState(i).GroupCd = "" ''idList(i).GroupCd
                labelState(i).SysItemCd = idList(i).SysItemCd
                labelState(i).OrderCd = idList(i).OrderCd
                labelState(i).OrderQty = idList(i).OrderQty
                labelState(i).InBoxQty = idList(i).InBoxQty
                labelState(i).IsFullBox = False ''idList(i).IsFullBox
                labelState(i).ReadText = ""
                labelState(i).LabelRnk = "P"
                labelState(i).Begin = True

                ReDim labelState(i).actionLabelRecord(0)
                With labelState(i).actionLabelRecord(0)

                    .ActionDate = readDateTime.ToString("yy/MM/dd")
                    .ActionTime = readDateTime.ToString("HH:mm:ss")
                    .LabelCate = CCommon.getStrLabelType2(CCommon.LABEL_DNC)
                    .LabelRnk = "P"
                    .LabelData = readText
                    .LabelState = "F"
                    .ActionQty = idList(i).InBoxQty
                    .EmpCd = pdaTask.EmpInfo.empNo
                    .ErrorCd = "OK"
                End With
            Next
            Dim retStr() As String = pdaTask.commitINNERDELIVERY(labelState)
            Dim errMsg As String = ""

            For i = 0 To retStr.Length - 1
                If retStr(i) <> "" Then
                    errMsg += retStr(i) & vbNewLine
                End If
            Next

            If errMsg <> "" Then
                ''警告
                errMsg = CCommon.CNS_MSG_050 & vbNewLine & vbNewLine & errMsg

                ''警告
                Dim result As String = ""
                Dim prevWindowMode As EnumWindowMode
                Do While result <> "[OK]"
                    If result = "" Then
                        pdaTask.Hardware.alert()
                        prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                        pdaTask.WindowManager.SetAlertMessage(errMsg, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                        pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                    End If
                    result = task.ReadTextWait(-1)
                Loop
                pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
            End If
        Else
            ''警告
            Dim result As String = ""
            Dim prevWindowMode As EnumWindowMode
            Do While result <> "[OK]"
                If result = "" Then
                    pdaTask.Hardware.alert()
                    prevWindowMode = pdaTask.WindowManager.GetWindowMode()
                    pdaTask.WindowManager.SetAlertMessage(altMsg, CCommon.COLOR_BLACK, CCommon.COLOR_WHITE)
                    pdaTask.WindowManager.ChangeWindowMode(EnumWindowMode.ALERT_MODE)
                End If
                result = task.ReadTextWait(-1)
            Loop
            pdaTask.WindowManager.ChangeWindowMode(prevWindowMode)
        End If
        task.ActionCd = "LABEL"
        task.ProcessText = ""
        nextState = GetNextState(task)
        Return nextState
    End Function

    ''' <summary>
    ''' 終了メソッド(未使用)
    ''' </summary>
    ''' <param name="task"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function DoExit(ByVal task As CTaskBase) As Boolean
        Debug.WriteLine("CINNERDELIVERYState.DoExit")
        Return True
    End Function

End Class
